import {component,request,layer,loading,util} from 'nuijs';
module.export = module.extend(component,{
    _options:{

    },
    _exec:function(){
        let opt = this._options;
        let onInit = opt.onInit;
        delete opt.onInit;
        let valid = opt.valid;
        let def = layer.config('button');
        let button = opt.button;
        //给表格绑定表单校验
        let form = $(opt.target);//表单
        let self = form.closest('.nui-layer')[0].nui.layer;
        let main = self.main;
        let elems = main.find('[name!=""][data-rule]');
        let rules = {};
        let messages = {};
        elems.each(function(){
            let elem = $(this);
            let name = elem.attr('name');
            let data = elem.data();
            let rule = eval('('+ data.rule +')');
            rules[name] = rule;
            if(typeof data.message !== 'undefined'){
                let message = eval('('+ data.message +')');
                $.each(message, function(key, msg){
                    if(typeof opt.messageFilter === 'function'){
                        message[key] = opt.messageFilter.call(opt, name, msg)||''
                    }
                })
                messages[name] = message;
            } 
        });
        let opts = {
            rules:rules,
            messages:messages,
            errorClass:'s-err',
            onkeyup:false,
            focusInvalid:false,
            onfocusout:false,
            focusCleanup:true,
            ignore:'',
            success:function(error, element) {
                error.remove();
                $(element).addClass('s-succ');
            },
            errorPlacement:function(error, element) {
                element.removeClass('s-succ');
                if(error.text()){
                    element.closest(opt.itemWrap||'.ui-item').find(opt.errorWrap||'.ui-err').html(error);
                }
            },
            submitHandler:function(){
                let param = {};
                if(typeof opt.getData === 'function'){
                    param = opt.getData.call(opt, self, form)
                }
                else{
                    param = util.getData(form, '').result;
                }

                if(typeof opt.onBeforeSubmit === 'function'){
                    param = opt.onBeforeSubmit.call(opt, self, param);
                    if(param === false){
                        return false
                    }
                }

                let _loading = loading({
                    content:opt.loading||'正在保存数据...',
                    under:self
                });
                
                request($.extend({
                    url:form.attr('action'),
                    dataType:'json',
                    type:'POST',
                    data:param,
                    success:function(res, xhr){
                        _loading.hide();
                        if(typeof opt.onSuccess === 'function'){
                            opt.onSuccess.call(opt, self, res, xhr, param)
                        }
                    },
                    error:function(xhr){
                        _loading.hide();
                        if(typeof opt.onError === 'function'){
                            opt.onError.call(opt, self, xhr, param)
                        }
                    }
                }, opt.ajax||{}), null)
            }
        }
        self.validator = form.validate(Nui.extend(true, opts, valid||{}));
        typeof onInit === 'function' && onInit.call(opt, self)
    }
})
